﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Logic.Interface;
using System.Collections.Generic;
public partial class Score_Education_AreaManage_SituationManager : System.Web.UI.Page
{
    /// <summary>
    /// 局级考试的管理界面，除了新建以外的所有功能
    /// </summary>
    protected int currSchoolYear = 0, currTerm = 0,currentOpenEXAM = 0,areaId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    private void InitYearAndTerm()
    {
        IGlobalTermInfoLogic globalTermInfoLogic = Container<IGlobalTermInfoLogic>.Instance;
        List<GlobalTermInfo> globalTermInfoList = (List<GlobalTermInfo>)globalTermInfoLogic.FindAll();
        if (globalTermInfoList.Count == 0)
            return;

        currSchoolYear = globalTermInfoList[0].GlobalYear;
        currTerm = globalTermInfoList[0].GlobalTerm;
        
    }

    //初始化 repeat控件，集成显示教师关联信息
    protected void btnBind_Click(object sender, EventArgs e)
    {
        Bind();
    }
    private void Bind()
    {
        currentOpenEXAM = EducationCookie.ExamID;//获取当前考试id
        InitYearAndTerm();

        string s = "";
        s = "( areaid=" + CommonScore.GetAreaID + " or areaid=0 )";
        Hashtable table = new Hashtable();
        table.Add("AreaIDex", s);

        IExamsEducationLogic eduExamLogic = Container<IExamsEducationLogic>.Instance;
        List<ExamsEducation> list = (List<ExamsEducation>)eduExamLogic.FindAll(table);
        CommonScore.ConvertEduExamHtmlCode(ref list);

        List<ExamsEducation> targetList = new List<ExamsEducation>();
        int year = 0, term = 0;
        if (string.IsNullOrEmpty(txtYear.Text))
        {
            if (currSchoolYear > 0 && currTerm > 0)
            {
                year = currSchoolYear;
                term = currTerm;
            }
        }
        else
        {
            int.TryParse(txtYear.Text, out year);
            int.TryParse(txtTerm.Text, out term);
        }
        if (year > 0)
        {
            if (term > 0)
            {
                foreach (ExamsEducation item in list)
                {
                    if (item.SchoolYear == year && item.Term == term)
                        targetList.Add(item);
                }
            }
            else
            {
                foreach (ExamsEducation item in list)
                {
                    if (item.SchoolYear == year)
                        targetList.Add(item);
                }
            }
            Repeater1.DataSource = targetList;
        }
        else
        {
            Repeater1.DataSource = list;
        }

        Repeater1.DataBind();
    }

 
    /// <summary>
    /// 获取对应考试的科目
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static List<ExamSubjecyEducation> GetSubjectsByExam(int examId)
    {
        IExamSubjecyEducationLogic subjectLogic = Container<IExamSubjecyEducationLogic>.Instance;
        Hashtable table = new Hashtable();
        table.Add("ExamID", examId);

        List<ExamSubjecyEducation> list = (List<ExamSubjecyEducation>)subjectLogic.FindAll(table);
        return list;
    }

    /// <summary>
    /// 删除局级考试
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int DelEduExam(int examId)
    {
        IExamsEducationLogic eduExamLogic = Container<IExamsEducationLogic>.Instance;
        int n = eduExamLogic.DelEduExam(examId);
        return n;
    }

    /// <summary>
    /// 下发考试
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int PublishedExam(int examId)
    {
        IExamsEducationLogic eduExamLogic = Container<IExamsEducationLogic>.Instance;
        int n = eduExamLogic.PublishedExam(examId);
        return n;
    }


    /// <summary>
    /// 结束考试
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int CloseEduExam(int examId)
    {
        IExamsEducationLogic eduExamLogic = Container<IExamsEducationLogic>.Instance;
        int n = eduExamLogic.ClosedExam(examId);
        return n;
    }


    protected void btnSelect_Click(object sender, EventArgs e)
    {
        int year = -1, term = -1, grade = -1, state = -1;
        Hashtable table = new Hashtable();
        string s = "";

        s = txtYear.Text;
        if (!String.IsNullOrEmpty(s))
        {
            year = Convert.ToInt32(s);
            if(year > 0)
                table.Add("SchoolYear", year);
        }
        s = txtTerm.Text;
        if (!String.IsNullOrEmpty(s))
        {
            term = Convert.ToInt32(s);
            if (term > 0)
                table.Add("Term", term);
        }
        s = txtGrade.Text;
        if (!String.IsNullOrEmpty(s))
        {
            grade = Convert.ToInt32(s);
            if(grade > 0)
                table.Add("Grade", grade);
        }
        s = txtState.Text;
        if (!String.IsNullOrEmpty(s))
        {
            state = Convert.ToInt32(s);
            if(state > -1)
                table.Add("State", state);
        }
        
        s = "( areaid=" + CommonScore.GetAreaID + " or areaid=0 )";
        table.Add("AreaIDex", s);

        IExamsEducationLogic eduExamLogic = Container<IExamsEducationLogic>.Instance;
        List<ExamsEducation> list = (List<ExamsEducation>)eduExamLogic.FindAll(table);

        currentOpenEXAM = EducationCookie.ExamID;//获取当前考试id

        Repeater1.DataSource = list;
        Repeater1.DataBind();
    }


    #region 学年、学期、年级、状态的转换
    public string ConvertGrade(int grade)
    {
        return CommonScore.ConvertGradeNum(grade);
    }
    public string ConvertTerm(short term)
    {
        return CommonScore.ConvertTerm(term);
    }
    public string ConvertClassType(int classType)
    {
        return CommonScore.ConvertClassType(classType);
    }
    public string ConvertState(int state)
    {
        return CommonScore.ConvertTestStatus(state);
    }
    #endregion


    protected void btnSetCookie_Click(object sender, EventArgs e)
    {
       // SetCookie(txtExamId.Text, txtExamName.Text, txtGrade.Text, txtClassType.Text);
    }

    /// <summary>
    /// 打开考试，设置考试的cookie信息
    /// </summary>
    /// <param name="examId"></param>
    /// <param name="examName"></param>
    /// <param name="grade"></param>
    /// <param name="classType"></param>
    [System.Web.Services.WebMethod]
    public static void SetCookie(string examId, string examName, string grade, string classType)
    {
        int nExamId = 0, nGrade = 0, nClassType = 0;
        if (!String.IsNullOrEmpty(examId))
        {
            if (int.TryParse(examId, out nExamId))
                nExamId = Convert.ToInt32(examId);
        }
        if (!String.IsNullOrEmpty(grade))
        {
            if (int.TryParse(grade, out nGrade))
                nGrade = Convert.ToInt32(grade);
        }
        if (!String.IsNullOrEmpty(classType))
        {
            if (int.TryParse(classType, out nClassType))
                nClassType = Convert.ToInt32(classType);
        }

        if (nExamId > 0 && nGrade > 0)
        {
            EducationCookie.ClearExamInfo();
            bool b = EducationCookie.SetOpenedExam(nExamId, examName, nGrade, nClassType);
        }
    }

    /// <summary>
    /// 获取打开的考试名
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    public string GetOpenedExamName()
    {
        string examName = EducationCookie.ExamName;
        return examName;
    }


}
